package com.zrrd;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author WanJl
 * @version 1.0
 * @title XXX
 * @description
 * @create 2024/8/18
 */
public class XXX {
    static class TourSpot {
        int m;
        int n;
        int x;
        int y;

        public TourSpot(int m, int n, int x, int y) {
            this.m = m;
            this.n = n;
            this.x = x;
            this.y = y;
        }
    }

    public static double calculateComfortValue(int t, int k, TourSpot[] spots) {
        double maxComfortValue = Double.MIN_VALUE;
        int[] indices = new int[t];
        for (int i = 0; i < t; i++) {
            indices[i] = i;
        }

        // 生成所有可能的旅游景点组合
        while (true) {
            if (Arrays.stream(indices).distinct().count()!= k) {
                break;
            }
            int[] path = new int[k + 2];
            path[0] = 0;
            int index = 1;
            for (int i : indices) {
                path[index++] = i;
            }
            path[k + 1] = 0;

            double cm = 0;
            double cn = 0;
            for (int i = 0; i < k + 1; i++) {
                int x1 = spots[path[i]].x;
                int y1 = spots[path[i]].y;
                int x2 = spots[path[i + 1]].x;
                int y2 = spots[path[i + 1]].y;
                cm += Math.abs(x1 - x2) + Math.abs(y1 - y2);
                cn += spots[path[i + 1]].n;
            }

            double comfortValue = cn / cm;
            maxComfortValue = Math.max(maxComfortValue, comfortValue);

            if (!nextPermutation(indices)) {
                break;
            }
        }

        return maxComfortValue;
    }

    public static boolean nextPermutation(int[] nums) {
        int i = nums.length - 2;
        while (i >= 0 && nums[i] >= nums[i + 1]) {
            i--;
        }
        if (i >= 0) {
            int j = nums.length - 1;
            while (j >= 0 && nums[i] >= nums[j]) {
                j--;
            }
            swap(nums, i, j);
        }
        reverse(nums, i + 1, nums.length - 1);
        return i >= 0;
    }

    public static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public static void reverse(int[] nums, int start, int end) {
        while (start < end) {
            swap(nums, start, end);
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        int t = 3;
        int k = 2;
        TourSpot[] spots = new TourSpot[]{
                new TourSpot(6, 20, 10, 15),
                new TourSpot(-5, 10, 30, 27),
                new TourSpot(-5, 10, 20, 25)
        };
        double comfortValue = calculateComfortValue(t, k, spots);
        System.out.printf("%.6f", comfortValue);
    }
}